package ve.gob.ine.censo.beans;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class TripleDES {

	private static byte[] sharedkey = "GQ0112H&P102471.NIV%ANIA".getBytes();

	private static byte[] sharedvector = {Byte.MAX_VALUE, 0, 1, 15, 87, 86, 45, 79};

//	static String clave = "123478785";
//	static String ciphertext = "z3FjBondZ+DrQoA1x3OArw==";
	
	public static void main(String... argv)
	    throws Exception
	  {
		String clave = "19340101";
//		encripteTEST(clave);
//		                                   "z3FjBondZ+DrQoA1x3OArw=="
//		                                        "2pxfpaEuZ9gztGLxPcdhmhQ=="
		desencripteTEST("z3FjBondZ+DrQoA1x3OArw==",clave); 
		
	  }
	
	public static void encripteTEST(String clave) throws Exception{
		String encriptado = encrypt(clave);
		System.out.println(encriptado);
		desencripteTEST(encriptado, clave);
	}
	
	public static void desencripteTEST(String ciphertext, String clave) throws Exception{
	    String decrypt =  decrypt(ciphertext);
	    System.out.println(ciphertext + "  " + decrypt);
	    decrypt = decrypt.substring(1, decrypt.length());
	    System.out.println(ciphertext + "  " + decrypt);
	    System.out.println(clave.contains(decrypt));
	    System.out.println(clave);
		
	}

	  public static String encrypt(String plaintext) throws Exception {
		    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
		    c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sharedkey, "DESede"), new IvParameterSpec(sharedvector));
		    String encriptado = new String (Base64Coder.encode(c.doFinal(plaintext.getBytes())));
		    return encriptado.substring(1, encriptado.length());
		    
	  }
	  
//	  public static String encrypt(String plaintext) throws Exception {
//	    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
//	    c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sharedkey, "DESede"), new IvParameterSpec(sharedvector));
//	    return Base64Coder.encode(c.doFinal(plaintext.getBytes())).toString();
//	  }
	  
	  public static String decrypt(String ciphertext) throws Exception {
	    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
	    c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sharedkey, "DESede"), new IvParameterSpec(sharedvector));
	    return new String(c.doFinal(Base64Coder.decode(ciphertext)));
	  }

	}
